<?php

namespace app\common\traits\controller;

use Carbon\Carbon;
use think\Cache;
use think\Session;
use think\Config;
use think\Request;
use app\common\Lang;
use think\exception\HttpException;

trait TraitController
{
    protected static $loginuser = '';
    private $key = 'b456b465f7';

    public function _initialize()
    {
        if (!Config::get('lang_switch_on')) {
            echo 'open lang_switch_on';
            exit;
        }

        session(['prefix' => '','auto_start' => true, 'domain' => config('url_domain_root')]);

        $this->init();        
    }
    /**
     * 初始化操作
     * @return [type] [description]
     */
    protected function init(){}

    protected function first()
    {
        //get user info
        $this->assign('title','双辉');
        $this->assign('name','sanyc');
        $this->assign('f','sanyc');
    }

    public function clearCache()
    {
        return Cache::clear(); 
    }
    /**
     * 设置系统语言
     * @param [type] $lang [description]
     */
    public function setAppLang($lang = '')
    {
        $lang = $lang ? $lang : Session::get('user.language');
        if ($lang) {
            $lang == Session::get('user.language') ? : Session::set('user.language', $lang);
            Lang::setLangCookie($lang);
        } 
    }
    /**
     * 校验sign
     * @return [type] [description]
     */
    protected function checkToken($data = null)
    {
        $data = $data ? : $this->request->get();
        
        if (isset($data['sign']) && $data['sign'] === $this->mixSign($data)) {
            return true;
        }
        
        return false;
    }
    /**
     * 组合sign
     * @param  [type] $data [description]
     * @return [type]       [description]
     */
    protected function mixSign($data)
    {
        $data = array_diff_key($data, array_flip(['_url', 'sign', 's', '_token', 'action']));
        ksort($data);

        $str = '';
        foreach ($data as $k => $value) {
            $value = urldecode($value);
            if ($value || $value === '0') {
                $str .= $k . $value;
            }
        }

        $str .= 'key' . $this->key;
        return md5($str);
    }
    /**
     * 获取登录用户信息
     * @param  string $field [description]
     * @return [type]        [description]
     */
    public function getLoginUserInfo($field = '')
    {
    	if (empty(self::$loginuser)) {
            $user_model = isset(self::$user_model) ? self::$user_model : 'Users';
            self::$loginuser = model($user_model)->getLoginUserInfo();
        }
        if ($field) {
            if (isset(self::$loginuser[$field])) {
                return self::$loginuser[$field]; 
            }
            return false;
        }
        return self::$loginuser;
    }
    public static function getSettings($short_name, $field = '', $array = false)
    {
        if (is_bool($field)) {
            $array = $field;
            $field = '';
        }
        return model('Settings')->getSettings($short_name, $field, $array);
    }
    public static function flash()
    {
        Session::flash('_old_input', Request::instance()->param());
    }
    public static function old($value = '')
    {
        if ($value) {
            return Session::get('_old_input.'.$value);
        }
        return Session::get('_old_input');
        
    }
}
